$(function(){
});

//=================================================翻页
var items_per_page = 10;
var scroll_in_progress = false;
var myScroll;
load_content = function(refresh, next_page) {
    if(!next_page) {
        next_page = 1;
    }

	var userUuid = dataGet("userUuid");
 	var url = urlfile + "corePointsOrder/find/my/pointsorder";
    var data = "&crporUser=" + userUuid + "&pageNum=" + next_page + "&pageSize=" + items_per_page;

    ajaxTool("post",data,url,
        function error(XMLHttpRequest, textStatus, errorThrown,fnErr){
            alert("error:" + XMLHttpRequest);
        },
        function success(data){
            if(!data.success) {
                alert(data.errMsg);
            }else{
            	for(var i=0; i<data.data.result.length; i++) {
                    
                    var record_img_type="";
                    if(data.data.result[i].list.length>0){
                    	record_img_type = 'style="background: url('+ urlfile +'coreAttachment/image/get/' + data.data.result[i].list[0].cratmUuid + ') no-repeat;-webkit-background-size: 71px 65px;background-size:  71px 65px;"';
                    }

                    var status = data.data.result[i].crporStatus;
                    var statusStr = "";

	            	if(1==status){
	            		statusStr="支付中";
	            	}

	            	if(2==status){
	            		statusStr="支付成功";
	            	}

                    if(3==status){
                        statusStr="支付失败";
                    }

                    if(4==status){
                        statusStr="已发货";
                    }

                    if(5==status){
                        statusStr="交易成功";
                    }

	            	var item = '<li><div class="store_order_item">'+
                                    '<div class="record_img"' + record_img_type + '></div>'+
                                    '<div class="store_order_content">'+                                       
                                        '<div class="crn_middle">'+
                                            '<span class="store_order_price">'+ data.data.result[i].crporGoodsName +'</span>'+
                                            '<div class="crn_store_order_time">'+ data.data.result[i].crporCdate +'</div>'+
                                            '<div class="store_order_status_border">'+statusStr+'</div>' +
                                        '</div>'+
                                    '</div> '+ 
                                '</div></li>';
					$('#wrapper > #scroller > ul').append(item);
            	}
            
                if (refresh) {
                    
                    myScroll.refresh();
                    pullActionCallback();
                    
                } else {
                    
                    if (myScroll) {
                        myScroll.destroy();
                        $(myScroll.scroller).attr('style', ''); // Required since the styles applied by IScroll might conflict with transitions of parent layers.
                        myScroll = null;
                    }
                    trigger_myScroll();
                    
                }
            }
    });
    
};



function pullDownAction() {
    load_content('refresh');
    $('#wrapper > #scroller > ul').data('page', 1);
    
    // Since "topOffset" is not supported with iscroll-5
    $('#wrapper > .scroller').css({top:0});
    
}
function pullUpAction(callback) {
	var next_page;
    if ($('#wrapper > #scroller > ul').data('page')) {
        next_page = parseInt($('#wrapper > #scroller > ul').data('page'), 10) + 1;
    } else {
        next_page = 2;
    }
    load_content('refresh', next_page);
    $('#wrapper > #scroller > ul').data('page', next_page);
    
    if (callback) {
        callback();
    }
}
function pullActionCallback() {
    if (pullDownEl && pullDownEl.className.match('loading')) {
        
        pullDownEl.className = 'pullDown';
        pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh';
        
        myScroll.scrollTo(0, parseInt(pullUpOffset)*(-1), 200);
        
    } else if (pullUpEl && pullUpEl.className.match('loading')) {
        
        $('.pullUp').removeClass('loading').html('');
        
    }
}

var pullActionDetect = {
    count:0,
    limit:10,
    check:function(count) {
        if (count) {
            pullActionDetect.count = 0;
        }
        // Detects whether the momentum has stopped, and if it has reached the end - 200px of the scroller - it trigger the pullUpAction
        setTimeout(function() {
            if (myScroll.y <= (myScroll.maxScrollY + 200) && pullUpEl && !pullUpEl.className.match('loading')) {
                $('.pullUp').addClass('loading').html('<span class="pullUpIcon">&nbsp;</span><span class="pullUpLabel">Loading...</span>');
                pullUpAction();
            } else if (pullActionDetect.count < pullActionDetect.limit) {
                pullActionDetect.check();
                pullActionDetect.count++;
            }
        }, 200);
    }
};

function trigger_myScroll(offset) {
    pullDownEl = document.querySelector('#wrapper .pullDown');
    if (pullDownEl) {
        pullDownOffset = pullDownEl.offsetHeight;
    } else {
        pullDownOffset = 0;
    }
    pullUpEl = document.querySelector('#wrapper .pullUp');  
    if (pullUpEl) {
        pullUpOffset = pullUpEl.offsetHeight;
    } else {
        pullUpOffset = 0;
    }
    
    if ($('#wrapper ul > li').length < items_per_page) {
        // If we have only 1 page of result - we hide the pullup and pulldown indicators.
        $('#wrapper .pullDown').hide();
        $('#wrapper .pullUp span').hide();
        offset = 0;
    } else if (!offset) {
        // If we have more than 1 page of results and offset is not manually defined - we set it to be the pullUpOffset.
        offset = pullUpOffset;
    }
    
    myScroll = new IScroll('#wrapper', {
        probeType:1, tap:true, click:false, preventDefaultException:{tagName:/.*/}, mouseWheel:true, scrollbars:true, fadeScrollbars:true, interactiveScrollbars:false, keyBindings:false,
        deceleration:0.0002,
        startY:(parseInt(offset)*(-1))
    });
    
    myScroll.on('scrollStart', function () {
        scroll_in_progress = true;
    });
    myScroll.on('scroll', function () {
        
        scroll_in_progress = true;
        
        if ($('#wrapper ul > li').length >= items_per_page) {
            if (this.y >= 5 && pullDownEl && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'pullDown flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh';
                this.minScrollY = 0;
            } else if (this.y <= 5 && pullDownEl && pullDownEl.className.match('flip')) {
                pullDownEl.className = 'pullDown';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh';
                this.minScrollY = -pullDownOffset;
            }
            
            console.log(this.y);
            pullActionDetect.check(0);
            
        }
    });
    myScroll.on('scrollEnd', function () {
        console.log('scroll ended');
        setTimeout(function() {
            scroll_in_progress = false;
        }, 100);
        if ($('#wrapper ul > li').length >= items_per_page) {
            if (pullDownEl && pullDownEl.className.match('flip')) {
                pullDownEl.className = 'pullDown loading';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';
                pullDownAction();
            }
            // We let the momentum scroll finish, and if reached the end - loading the next page
            pullActionDetect.check(0);
        }
    });
    
    // In order to prevent seeing the "pull down to refresh" before the iScoll is trigger - the wrapper is located at left:-9999px and returned to left:0 after the iScoll is initiated
    setTimeout(function() {
        $('#wrapper').css({left:0});
    }, 100);
}

function loaded() {

    load_content();

}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);